package com.nanjia.boot.batch_stock.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nanjia.boot.batch_stock.converter.WarehouseBatchStockConverter;
import com.nanjia.boot.batch_stock.mapper.WarehouseBatchStockMapper;
import com.nanjia.boot.batch_stock.model.entity.WarehouseBatchStock;
import com.nanjia.boot.batch_stock.model.form.WarehouseBatchStockForm;
import com.nanjia.boot.batch_stock.model.query.WarehouseBatchStockQuery;
import com.nanjia.boot.batch_stock.model.vo.WarehouseBatchStockVO;
import com.nanjia.boot.batch_stock.service.WarehouseBatchStockService;
import com.nanjia.boot.common.result.Result;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 库存记录服务实现类
 *
 * @author 周洋
 * @since 2025-08-22 11:27
 */
@Service
@RequiredArgsConstructor
public class WarehouseBatchStockServiceImpl extends ServiceImpl<WarehouseBatchStockMapper, WarehouseBatchStock> implements WarehouseBatchStockService {

    private final WarehouseBatchStockConverter warehouseBatchStockConverter;

    /**
    * 获取库存记录分页列表
    *
    * @param queryParams 查询参数
    * @return {@link IPage<WarehouseBatchStockVO>} 库存记录分页列表
    */
    @Override
    public IPage<WarehouseBatchStockVO> getWarehouseBatchStockPage(WarehouseBatchStockQuery queryParams) {
        Page<WarehouseBatchStockVO> pageVO = this.baseMapper.getWarehouseBatchStockPage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }
    
    /**
     * 获取库存记录表单数据
     *
     * @param id 库存记录ID
     * @return 库存记录表单数据
     */
    @Override
    public WarehouseBatchStockForm getWarehouseBatchStockFormData(Long id) {
        WarehouseBatchStock entity = this.getById(id);
        return warehouseBatchStockConverter.toForm(entity);
    }
    
    /**
     * 新增库存记录
     *
     * @param formData 库存记录表单对象
     * @return 是否新增成功
     */
    @Override
    public boolean saveWarehouseBatchStock(WarehouseBatchStockForm formData) {
        WarehouseBatchStock entity = warehouseBatchStockConverter.toEntity(formData);
        return this.save(entity);
    }
    
    /**
     * 更新库存记录
     *
     * @param id   库存记录ID
     * @param formData 库存记录表单对象
     * @return 是否修改成功
     */
    @Override
    public boolean updateWarehouseBatchStock(Long id,WarehouseBatchStockForm formData) {
        WarehouseBatchStock entity = warehouseBatchStockConverter.toEntity(formData);
        return this.updateById(entity);
    }
    
    /**
     * 删除库存记录
     *
     * @param ids 库存记录ID，多个以英文逗号(,)分割
     * @return 是否删除成功
     */
    @Override
    public boolean deleteWarehouseBatchStocks(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的库存记录数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

    @Override
    public Result<List<WarehouseBatchStockVO>> getList(WarehouseBatchStockQuery queryParams) {

        List<WarehouseBatchStockVO> list = this.baseMapper.getList(queryParams);
        return Result.success(list);

    }
}
